package com.czmh.etc.onlinepubsys.utils;

import com.alibaba.fastjson.JSON;
import com.czmh.etc.onlinepubsys.exceptions.EtcMsgException;
import com.czmh.etc.onlinepubsys.json.JsonResult;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.codec.binary.StringUtils;

import java.beans.PropertyDescriptor;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*
 * JsonResult工具类
 */
@Slf4j
public class JsonResultUtil {
    private JsonResultUtil() {
    }

    public static JsonResult toResult() {
        return toResult(null, "操作成功", true, true);
    }

    public static JsonResult toResult(boolean encode) {
        return toResult(null, "操作成功", encode, true);
    }

    public static JsonResult toErrorResult(String message) {
        return toResult(null, message, true, false);
    }

    public static JsonResult toResult(Object data) {
        return toResult(data, "操作成功", true, true);
    }

    /**
     * @param data
     * @param message
     * @return
     */
    public static JsonResult toResult(Object data, String message) {
        return toResult(data, message, true, true);
    }

    public static JsonResult toResult(Object data, String message, boolean encode) {
        return toResult(data, message, encode, true);
    }

    /**
     * @param data
     * @param message
     * @return
     */
    public static JsonResult toResult(Object data, String message, boolean encode, boolean status) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setEncode(encode);
        jsonResult.setStatus(status);
        //如果是分页需要设置总条数total
        if (data instanceof Page) {
            Page page = (Page) data;
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(page.getResult());
        } else if (data instanceof Collection) {
            Collection list = (Collection) data;
            jsonResult.setTotal(list.size());
            jsonResult.setData(list);
        } else {
            jsonResult.setTotal(1);
            jsonResult.setData(data);
        }
        jsonResult.setMessage(message);
        return jsonResult;
    }

    /**
     * @param data
     * @param message
     * @return
     */
    public static JsonResult toResult(Object data, String message, Object code, boolean encode, boolean status) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setEncode(encode);
        jsonResult.setCode(code);
        jsonResult.setStatus(status);
        //如果是分页需要设置总条数total
        if (data instanceof Page) {
            Page page = (Page) data;
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(page.getResult());
        } else if (data instanceof Collection) {
            Collection list = (Collection) data;
            jsonResult.setTotal(list.size());
            jsonResult.setData(list);
        } else {
            jsonResult.setTotal(1);
            jsonResult.setData(data);
        }
        jsonResult.setMessage(message);
        return jsonResult;
    }


    /**
     * 返回base64位数据
     *
     * @param data
     * @param message
     * @return
     */
    public static JsonResult toJsonBase64Result(Object data, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatus(true);
        Object resultData;
        //如果是分页需要设置总条数total
        if (data instanceof Page) {
            Page page = (Page) data;
            jsonResult.setTotal(page.getTotal());
            resultData = page.getResult();
        } else if (data instanceof Collection) {
            Collection list = (Collection) data;
            jsonResult.setTotal(list.size());
            resultData = list;
        } else {
            jsonResult.setTotal(1);
            resultData = data;
        }
        try {
            String baseStr = new String(org.apache.commons.codec.binary.Base64.encodeBase64(JSON.toJSONString(resultData).getBytes(StandardCharsets.UTF_8)));
            jsonResult.setData(baseStr);
        } catch (Exception e) {
            throw new EtcMsgException("0001", "数据转换异常");
        }
        jsonResult.setMessage(message);
        return jsonResult;
    }


    /**
     * 说明：将实体bean转成Map,并在key前增加prefix对象名前缀
     * {
     * "org.id":"100",
     * "org.name":"分公司1"
     * }
     * created by huangbaidong 20160718
     *
     * @param prefix （字符串,如：“org.”）
     * @param bean   实体Bean, 如：Org
     * @return
     */
    public static Map<String, Object> beanToMap(String prefix, Object bean) {
        Map<String, Object> params = new HashMap<>(0);
        if (bean != null) {
            if (prefix == null) prefix = "";
            try {
                PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
                PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(bean);
                for (int i = 0; i < descriptors.length; i++) {
                    String name = descriptors[i].getName();
                    if (!StringUtils.equals(name, "class")) {
                        Object o = propertyUtilsBean.getNestedProperty(bean, name);
                        if (o instanceof Date) {
                            //默认日期类型多转换成yyyy-MM-dd, 如果需要不同格式, 在controller中覆盖key-value
                            params.put(prefix + name, DatetimeUtil.formatDate(((Date) o)));
                        } else {
                            params.put(prefix + name, o);
                        }
                    }
                }
            } catch (Exception e) {
                log.warn(e.getMessage(), e);
            }
        }
        return params;
    }

    //将javabean转为map类型，然后返回一个map类型的值
    public static Map<String, Object> beanToMap(Object bean) {
        return beanToMap("", bean);

    }
}
